package com.demo.zhlg.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.demo.zhlg.entity.Users;

import java.util.Calendar;
import java.util.Date;

public class JwtUtil {
    // 定义加密函数，参数为随机字符串用于加密
    private static final Algorithm algorithm = Algorithm.HMAC256("2dozl28%$%3=b3()ndka9duf==*");

    // 解密Token
    static public Object parseToken(String token, String key) {
        JWTVerifier jwtVerifier = JWT.require(algorithm).build();
        return jwtVerifier.verify(token).getClaim(key).as(Object.class);
    }

    //...
    // ...
    // 签发Token
    static public String generateToken(Users user) {
        Date start = new Date();
        Calendar calc = Calendar.getInstance();
        calc.add(Calendar.DATE, 1);
        Date end = calc.getTime(); // 截止日期
        return JWT.create()
                // 两个withClaim声明Payload放置的内容
                .withClaim("id", user.getUserId())
                .withClaim("username", user.getUsername())
                .withIssuedAt(start)
                .withExpiresAt(end)
                // 用定义的加密函数签名
                .sign(algorithm);
    }
}